﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PolePosition.v3.Desktop.Selectors
{
    public partial class SelectMapElements : DialogBase, IDialogBase
    {
        public SelectMapElements()
        {
            InitializeComponent();

            projects1.Dock = DockStyle.Fill;
            assetTypes1.Dock = DockStyle.Fill;
            xMissionProperties1.Dock = DockStyle.Fill;
            distributionProperties1.Dock = DockStyle.Fill;
            polygons1.Dock = DockStyle.Fill;
            meterProperties1.Dock = DockStyle.Fill;
            inspections1.Dock = DockStyle.Fill;

            projects1.BringToFront();
        }

        private void SelectMapElements_Load(object sender, EventArgs e)
        {
            LoadLastSelections();
        }

        public string Kml { get; private set; }

        private string m_XmissionGpsProjectList;
        private string m_XmissionInspectionProjectList;
        private string m_DistributionInspectionProjectList;
        private string m_DistributionGpsProjectList;
        private bool m_IncludeXmissionAssets;
        private bool m_IncludeDistributionAssets;
        private bool m_IncludeMeterAssets;

        private void LoadLastSelections()
        {
            //pre-load last selections made by the user
            projects1.SelectedProjectList = Program.UserSelections.UtilityMapCriteria.SelectedProjectList;
            assetTypes1.SelectedAssetTypeList = Program.UserSelections.UtilityMapCriteria.SelectedAssetTypeList;
            polygons1.SelectedMapList = Program.UserSelections.UtilityMapCriteria.SelectedMapList;

            xMissionProperties1.SelectedKvList = Program.UserSelections.UtilityMapCriteria.SelectedXmissionKvList;
            xMissionProperties1.SelectedTopTypeList = Program.UserSelections.UtilityMapCriteria.SelectedXmissionTopTypeList;
            xMissionProperties1.SelectedClassList = Program.UserSelections.UtilityMapCriteria.SelectedXmissionClassList;
            xMissionProperties1.SelectedSpeciesList = Program.UserSelections.UtilityMapCriteria.SelectedXmissionSpeciesList;
            xMissionProperties1.SelectedTreatmentList = Program.UserSelections.UtilityMapCriteria.SelectedXmissionTreatmentList;
            xMissionProperties1.SelectedYearList = Program.UserSelections.UtilityMapCriteria.SelectedXmissionYearList;

            distributionProperties1.SelectedPhaseList = Program.UserSelections.UtilityMapCriteria.SelectedDistributionPhaseList;
            distributionProperties1.SelectedTopTypeList = Program.UserSelections.UtilityMapCriteria.SelectedDistributionTopTypeList;
            distributionProperties1.SelectedClassList = Program.UserSelections.UtilityMapCriteria.SelectedDistributionClassList;
            distributionProperties1.SelectedSpeciesList = Program.UserSelections.UtilityMapCriteria.SelectedDistributionSpeciesList;
            distributionProperties1.SelectedTreatmentList = Program.UserSelections.UtilityMapCriteria.SelectedDistributionTreatmentList;
            distributionProperties1.SelectedYearList = Program.UserSelections.UtilityMapCriteria.SelectedDistributionYearList;

            inspections1.SelectedElectricalProblems = Program.UserSelections.UtilityMapCriteria.SelectedElectricalProblems;
            inspections1.SelectedInspectionNoteList = Program.UserSelections.UtilityMapCriteria.SelectedInspectionNoteList;
            inspections1.SelectedInspectionResultList = Program.UserSelections.UtilityMapCriteria.SelectedInspectionResultList;
            inspections1.SelectedInspectionTypeList = Program.UserSelections.UtilityMapCriteria.SelectedInspectionTypeList;
            inspections1.SelectedInspectorList = Program.UserSelections.UtilityMapCriteria.SelectedInspectorList;
            inspections1.SelectedVendorList = Program.UserSelections.UtilityMapCriteria.SelectedVendorList;


            meterProperties1.SelectedMeterNotesList = Program.UserSelections.UtilityMapCriteria.SelectedMeterNotesList;

        }

        private void GetSelections()
        {
            Program.UserSelections.UtilityMapCriteria.XmissionText = xMissionProperties1.comboBoxExPlacemarkText.Text;
            Program.UserSelections.UtilityMapCriteria.DistributionText = distributionProperties1.comboBoxExPlacemarkText.Text;

            //get selections made by the user
            Program.UserSelections.UtilityMapCriteria.SelectedProjectList = projects1.SelectedProjectList;
            Program.UserSelections.UtilityMapCriteria.SelectedAssetTypeList = assetTypes1.SelectedAssetTypeList;
            Program.UserSelections.UtilityMapCriteria.SelectedMapList = polygons1.SelectedMapList;

            Program.UserSelections.UtilityMapCriteria.SelectedXmissionKvList = xMissionProperties1.SelectedKvList;
            Program.UserSelections.UtilityMapCriteria.SelectedXmissionTopTypeList = xMissionProperties1.SelectedTopTypeList;
            Program.UserSelections.UtilityMapCriteria.SelectedXmissionClassList = xMissionProperties1.SelectedClassList;
            Program.UserSelections.UtilityMapCriteria.SelectedXmissionSpeciesList = xMissionProperties1.SelectedSpeciesList;
            Program.UserSelections.UtilityMapCriteria.SelectedXmissionTreatmentList = xMissionProperties1.SelectedTreatmentList;
            Program.UserSelections.UtilityMapCriteria.SelectedXmissionYearList = xMissionProperties1.SelectedYearList;

            Program.UserSelections.UtilityMapCriteria.SelectedDistributionPhaseList = distributionProperties1.SelectedPhaseList;
            Program.UserSelections.UtilityMapCriteria.SelectedDistributionTopTypeList = distributionProperties1.SelectedTopTypeList;
            Program.UserSelections.UtilityMapCriteria.SelectedDistributionClassList = distributionProperties1.SelectedClassList;
            Program.UserSelections.UtilityMapCriteria.SelectedDistributionSpeciesList = distributionProperties1.SelectedSpeciesList;
            Program.UserSelections.UtilityMapCriteria.SelectedDistributionTreatmentList = distributionProperties1.SelectedTreatmentList;
            Program.UserSelections.UtilityMapCriteria.SelectedDistributionYearList = distributionProperties1.SelectedYearList;

            Program.UserSelections.UtilityMapCriteria.SelectedElectricalProblems = inspections1.SelectedElectricalProblems;
            Program.UserSelections.UtilityMapCriteria.SelectedInspectionNoteList = inspections1.SelectedInspectionNoteList;
            Program.UserSelections.UtilityMapCriteria.SelectedInspectionResultList = inspections1.SelectedInspectionResultList;
            Program.UserSelections.UtilityMapCriteria.SelectedInspectionTypeList = inspections1.SelectedInspectionTypeList;
            Program.UserSelections.UtilityMapCriteria.SelectedInspectorList = inspections1.SelectedInspectorList;
            Program.UserSelections.UtilityMapCriteria.SelectedVendorList = inspections1.SelectedVendorList;

            Program.UserSelections.UtilityMapCriteria.SelectedMeterNotesList = meterProperties1.SelectedMeterNotesList;
        
            //have to break apart the projects into their appropriate types
            if (!string.IsNullOrEmpty(Program.UserSelections.UtilityMapCriteria.SelectedProjectList))
            {

                List<string> _XmissionGpsProjectList = new List<string>();
                List<string> _XmissionInspectionProjectList = new List<string>();
                List<string> _DistributionInspectionProjectList = new List<string>();
                List<string> _DistributionGpsProjectList = new List<string>();

                foreach (EntityLibrary.ProjectEntity _Project in projects1.SelectedProjects)
                {
                    if (_Project.ProjectTypeFk == Common.Constants.ProjectTypes.XmissionInspection)
                        _XmissionInspectionProjectList.Add(_Project.Vendor_ProjectId.ToString());
                    else if (_Project.ProjectTypeFk == Common.Constants.ProjectTypes.XmissionGpsVisit)
                        _XmissionGpsProjectList.Add(_Project.Vendor_ProjectId.ToString());
                    else if (_Project.ProjectTypeFk == Common.Constants.ProjectTypes.DistributionInspection)
                        _DistributionInspectionProjectList.Add(_Project.Vendor_ProjectId.ToString());
                    else if (_Project.ProjectTypeFk == Common.Constants.ProjectTypes.DistributionGpsVisit)
                        _DistributionGpsProjectList.Add(_Project.Vendor_ProjectId.ToString());
                }

                if (_XmissionGpsProjectList.Count() != 0)
                    m_XmissionGpsProjectList = string.Join(",", _XmissionGpsProjectList.ToArray());
                else
                    m_XmissionGpsProjectList = null;

                if (_XmissionInspectionProjectList.Count() != 0)
                    m_XmissionInspectionProjectList = string.Join(",", _XmissionInspectionProjectList.ToArray());
                else
                    m_XmissionInspectionProjectList = null;

                if (_DistributionInspectionProjectList.Count() != 0)
                    m_DistributionInspectionProjectList = string.Join(",", _DistributionInspectionProjectList.ToArray());
                else
                    m_DistributionInspectionProjectList = null;

                if (_DistributionGpsProjectList.Count() != 0)
                    m_DistributionGpsProjectList = string.Join(",", _DistributionGpsProjectList.ToArray());
                else
                    m_DistributionGpsProjectList = null;
            }
            else
            {
                m_XmissionGpsProjectList = null;
                m_XmissionInspectionProjectList = null;
                m_DistributionInspectionProjectList = null;
                m_DistributionGpsProjectList = null;
            }

            if (!string.IsNullOrEmpty(Program.UserSelections.UtilityMapCriteria.SelectedAssetTypeList))
            {
                List<Common.Enumerations.AssetTypes> _SelectedAssets;
                _SelectedAssets = assetTypes1.SelectedAssets;

                if (_SelectedAssets.Contains(Common.Enumerations.AssetTypes.Transmission))
                    m_IncludeXmissionAssets = true;
                else
                    m_IncludeXmissionAssets = false;

                if (_SelectedAssets.Contains(Common.Enumerations.AssetTypes.Distribution))
                    m_IncludeDistributionAssets = true;
                else
                    m_IncludeDistributionAssets = false;

                if (_SelectedAssets.Contains(Common.Enumerations.AssetTypes.Meters))
                    m_IncludeMeterAssets = true;
                else
                    m_IncludeMeterAssets = false;
            }
            else
            {
                m_IncludeXmissionAssets = false;
                m_IncludeDistributionAssets = false;
                m_IncludeMeterAssets = false;
            }

            //ListViewItem _Item;
            //_Item = listSelectorAssetType.ListItems.Add(Common.Enumerations.AssetTypes.Transmission.ToString());
            //_Item.Tag = Common.Enumerations.AssetTypes.Transmission;

            //_Item = listSelectorAssetType.ListItems.Add(Common.Enumerations.AssetTypes.Distribution.ToString());
            //_Item.Tag = Common.Enumerations.AssetTypes.Distribution;

            //_Item = listSelectorAssetType.ListItems.Add(Common.Enumerations.AssetTypes.Meters.ToString());
            //_Item.Tag = Common.Enumerations.AssetTypes.Meters;



        }

        #region Asynch Processing (Implements IDialogBase)

        public override void DoAsynchWork(DoWorkEventArgs e)
        {
            //retrieve the kml
            this.Kml = ServiceProxy.UtilityService.GetMapSearchCriteriaKMLString(
                Program.UserSelections.UtilityMapCriteria.XmissionText,
                Program.UserSelections.UtilityMapCriteria.DistributionText,
                m_XmissionGpsProjectList, 
                m_XmissionInspectionProjectList,
                m_DistributionInspectionProjectList, 
                m_DistributionGpsProjectList, 
                m_IncludeXmissionAssets, 
                m_IncludeDistributionAssets,
                m_IncludeMeterAssets, 
                Program.UserSelections.UtilityId,
                Program.UserSelections.UtilityMapCriteria.SelectedMapList,
                Program.UserSelections.UtilityMapCriteria.SelectedXmissionKvList,
                Program.UserSelections.UtilityMapCriteria.SelectedXmissionTopTypeList,
                Program.UserSelections.UtilityMapCriteria.SelectedXmissionSpeciesList,
                Program.UserSelections.UtilityMapCriteria.SelectedXmissionTreatmentList,
                Program.UserSelections.UtilityMapCriteria.SelectedXmissionClassList,
                Program.UserSelections.UtilityMapCriteria.SelectedXmissionYearList,
                Program.UserSelections.UtilityMapCriteria.SelectedDistributionPhaseList,
                Program.UserSelections.UtilityMapCriteria.SelectedDistributionTopTypeList,
                Program.UserSelections.UtilityMapCriteria.SelectedDistributionSpeciesList,
                Program.UserSelections.UtilityMapCriteria.SelectedDistributionTreatmentList,
                Program.UserSelections.UtilityMapCriteria.SelectedDistributionClassList,
                Program.UserSelections.UtilityMapCriteria.SelectedDistributionYearList,
                Program.UserSelections.UtilityMapCriteria.SelectedMeterNotesList,
                Program.UserSelections.UtilityMapCriteria.SelectedElectricalProblems,
                Program.UserSelections.UtilityMapCriteria.SelectedInspectionResultList,
                Program.UserSelections.UtilityMapCriteria.SelectedInspectionTypeList,
                Program.UserSelections.UtilityMapCriteria.SelectedInspectorList,
                Program.UserSelections.UtilityMapCriteria.SelectedVendorList,
                Program.UserSelections.UtilityMapCriteria.SelectedInspectionNoteList);
        }

        public override void AsychWorkComplete(object sender, RunWorkerCompletedEventArgs e)
        {
            base.AsychWorkComplete(sender, e);
            //do whatever needs to be done after asynch work complete (BackgroudWorker_RunWorkerCompleted)

            //do whatever needs to be done after asynch work complete (BackgroudWorker_RunWorkerCompleted)
            if (e.Cancelled)
            {
                this.DialogResult = DialogResult.Abort;
                return;
            }

            if (e.Error != null)
            {
                //todo: create some sort of error dialog and use it here instead of messagebx
                DevComponents.DotNetBar.MessageBoxEx.Show("Error retrieving map data.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            this.DialogResult = DialogResult.OK;
        }

        #endregion
  
        private void buttonItemProject_Click(object sender, EventArgs e)
        {
            projects1.BringToFront();
        }

        private void buttonItemAssets_Click(object sender, EventArgs e)
        {
            assetTypes1.BringToFront();
        }

        private void buttonItemTransmission_Click(object sender, EventArgs e)
        {
            xMissionProperties1.BringToFront();
        }
    
        private void buttonItemMaps_Click(object sender, EventArgs e)
        {
            polygons1.BringToFront();
        }

        private void buttonItemDistribution_Click(object sender, EventArgs e)
        {
            distributionProperties1.BringToFront();
        }

        private void buttonItemMeter_Click(object sender, EventArgs e)
        {
            meterProperties1.BringToFront();
        }

        private void buttonItemInspection_Click(object sender, EventArgs e)
        {
            inspections1.BringToFront();
        }

        private void buttonXOk_Click(object sender, EventArgs e)
        {
            GetSelections();

            if (string.IsNullOrEmpty(Program.UserSelections.UtilityMapCriteria.SelectedProjectList) && 
                    string.IsNullOrEmpty(Program.UserSelections.UtilityMapCriteria.SelectedAssetTypeList))
            {
                DevComponents.DotNetBar.MessageBoxEx.Show("You must either one project or one asset to continue.", "Invalid Selection", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            base.StartAsynchWork();
        }

        private void buttonXCancel_Click(object sender, EventArgs e)
        {
            base.CancelAsynchWork();
        }
    }
}
